/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sdc.event;

import sdc.net.server.appli.ApplicationServer;
import sdc.Address;
import sdc.component.type.ComponentType;
import sdc.net.server.comp.ComponentServerManager;
import java.util.Map;
import java.util.Map.Entry;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author masa
 */
public class AppSamplingEvent extends Event{

    private ApplicationServer appServer;
    private final double t = 1.0;
    private FileWriter out;


    public AppSamplingEvent(double eventTime,FileWriter out, ApplicationServer appServer){
        super(eventTime);
        this.out = out;
        this.appServer = appServer;
    }

    @Override
    public void fire(){
        double TATsum = 0.0;
        for(Entry<ComponentType,Address> entry : appServer.getUseComponentMap().entrySet()){
            TATsum += ComponentServerManager.INSTANCE.get(entry.getValue()).getTAT(entry.getKey());
        }
        try{
            out.write(TATsum /3.0 + ",");
        } catch(IOException e){}
//System.out.println(TATsum);
        EventQueue eventQueue = EventQueue.INSTANCE;
        eventQueue.addEvent(new AppSamplingEvent(eventQueue.getCurrentTime()+t,out,appServer));
    }
}
